<template>
	<view>
		<view class="mt-300 text-center">
			<view class="font-40">欢迎来到河口人才招聘</view>
			<view class="font-24 font-grey mt-10">登录/注册后查看海量工作机会</view>
			<view class="login-btn" @click="getUserProfile">微信一键登录</view>
			<view class="false-btn" @click="$goRoute.goTab('/pages/index/index')">返回主页</view>
			<view class="flex items-center flex-center font-grey font-24">
				<checkbox value="cb" checked="false" />我已阅读并同意 <text class="font-g">《用户协议》</text> 和 <text
					class="font-g">《隐私政策》</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getOpenid,
		login
	} from '../../api/common';
	export default {
		data() {
			return {
				cb: false,
				user: null
			};
		},
		methods: {
			getUserProfile(e) {
				let _this = this;
				wx.getUserProfile({
					desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: res => {
						_this.user = res.userInfo;
						_this.appLoginWx();
					}
				});
			},
			//登录
			appLoginWx() {
				// #ifdef MP-WEIXIN
				let _this = this;
				uni.getProvider({
					service: 'oauth',
					success: function(res) {
						if (~res.provider.indexOf('weixin')) {
							uni.login({
								provider: 'weixin',
								success: loginRes => {
									uni.showLoading({
										title: '登录中'
									})
									getOpenid({
										code: loginRes.code
									}).then(resd => {
										uni.hideLoading()
										if (resd.code === 200) {
											_this.wxlogin(resd.result)
										} else {
											uni.showToast({
												title: '微信登录授权失败',
												icon: 'none'
											});
										}
									})
								},
								fail: () => {
									uni.showToast({
										title: '微信登录授权失败',
										icon: 'none'
									});
								}
							});
						} else {
							uni.showToast({
								title: '请先安装微信或升级版本',
								icon: 'none'
							});
						}
					}
				});
				//#endif
			},
			wxlogin(openid) {
				const _this = this
				login({
					nickname: this.user.username,
					openid: openid
				}).then(res => {
					if (res.code === 200) {
						this.$storage.setStorageSync('token', res.result)
						this.$tips.showConfirmModal('登录成功', () => {
							uni.setStorageSync('userInfo', {
								username: _this.user.nickName,
								type: 0
							})
							uni.navigateBack()
						})
					} else {
						this.$tips.showConfirmModal('登录失败，请稍后再试', () => {})
					}
				})

			}
		}
	}
</script>

<style lang="less">
	.login-btn {
		width: 80vw;
		background-color: #46B4B1;
		text-align: center;
		color: white;
		padding-top: 30rpx;
		padding-bottom: 30rpx;
		border-radius: 50rpx;
		margin: 200rpx auto 20rpx auto;
	}

	.false-btn {
		width: 80vw;
		background-color: #fff;
		border: 1px solid #46B4B1;
		text-align: center;
		color: #46B4B1;
		padding-top: 30rpx;
		padding-bottom: 30rpx;
		border-radius: 50rpx;
		margin: 0rpx auto 30rpx auto;
	}
</style>